{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "504539b7",
   "metadata": {},
   "source": [
    "## XML 代理\n",
    "\n",
    "一些语言模型（如Anthropic的Claude）特别擅长推理/编写XML。这将介绍如何在提示时使用使用 XML 的代理。\n",
    "\n",
    "仅与非结构化工具一起使用;即接受单个字符串输入的工具。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f962764",
   "metadata": {},
   "source": [
    "```python\n",
    "ChatPromptTemplate(\n",
    "    input_variables=['agent_scratchpad', 'input', 'tools'], \n",
    "    partial_variables={'chat_history': ''}, \n",
    "    messages=[\n",
    "        HumanMessagePromptTemplate(\n",
    "            prompt=PromptTemplate(\n",
    "                input_variables=[\n",
    "                    'agent_scratchpad', \n",
    "                    'chat_history', \n",
    "                    'input', \n",
    "                    'tools'\n",
    "                ], \n",
    "                template=\"You are a helpful assistant. Help the user answer any questions.\\n\\nYou have access to the following tools:\\n\\n{tools}\\n\\nIn order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>\\nFor example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:\\n\\n<tool>search</tool><tool_input>weather in SF</tool_input>\\n<observation>64 degrees</observation>\\n\\nWhen you are done, respond with a final answer between <final_answer></final_answer>. For example:\\n\\n<final_answer>The weather in SF is 64 degrees</final_answer>\\n\\nBegin!\\n\\nPrevious Conversation:\\n{chat_history}\\n\\nQuestion: {input}\\n{agent_scratchpad}\"\n",
    "            )\n",
    "        )\n",
    "    ]\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c21185a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = '''You are a helpful assistant. Help the user answer any questions.\n",
    "\n",
    "You have access to the following tools:\n",
    "\n",
    "{tools}\n",
    "\n",
    "In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>\n",
    "For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:\n",
    "\n",
    "<tool>search</tool><tool_input>weather in SF</tool_input>\n",
    "<observation>64 degrees</observation>\n",
    "\n",
    "When you are done, respond with a final answer between <final_answer></final_answer>. For example:\n",
    "\n",
    "<final_answer>The weather in SF is 64 degrees</final_answer>\n",
    "\n",
    "Begin!\n",
    "\n",
    "Previous Conversation:\n",
    "\n",
    "{chat_history}\n",
    "\n",
    "Question: {input}\n",
    "\n",
    "{agent_scratchpad}\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ccd56fc",
   "metadata": {},
   "source": [
    "你是一个有用的助手。 帮助用户回答任何问题。\n",
    "\n",
    "您可以使用以下工具：\n",
    "\n",
    "{tools}\n",
    "\n",
    "为了使用工具，您可以使用 `<tool></tool>`和 `<tool_input></tool_input>` 标签。 然后您将收到 `<observation></observation>` 形式的响应\n",
    "例如，如果您有一个名为“search”的工具，可以运行谷歌搜索，为了搜索 SF 的天气，您将响应：\n",
    "```\n",
    "<tool>search</tool><tool_input>旧金山天气</tool_input>\n",
    "<observation>64度</observation>\n",
    "```\n",
    "完成后，请在 `<final_answer></final_answer>` 之间回复最终答案。 例如：\n",
    "\n",
    "`<final_answer>`旧金山的天气温度为 64 度`</final_answer>`\n",
    "\n",
    "开始！\n",
    "\n",
    "之前的对话：\n",
    "\n",
    "{chat_history}\n",
    "\n",
    "问题：{input}\n",
    "\n",
    "{agent_scratchpad}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "02802fcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = '''You are a helpful assistant. Help the user answer any questions.\n",
    "\n",
    "You have access to the following tools:\n",
    "\n",
    "weather: 根据城市获取天气数据\n",
    "\n",
    "In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>\n",
    "For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:\n",
    "\n",
    "<tool>search</tool><tool_input>weather in SF</tool_input>\n",
    "<observation>64 degrees</observation>\n",
    "\n",
    "When you are done, respond with a final answer between <final_answer></final_answer>. For example:\n",
    "\n",
    "<final_answer>The weather in SF is 64 degrees</final_answer>\n",
    "\n",
    "Begin!\n",
    "\n",
    "Previous Conversation:\n",
    "\n",
    "\n",
    "Question: 今天广州的天气\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "799705bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = '''You are a helpful assistant. Help the user answer any questions.\n",
    "\n",
    "You have access to the following tools:\n",
    "\n",
    "weather: 根据城市获取天气数据\n",
    "\n",
    "In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>\n",
    "For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:\n",
    "\n",
    "<tool>search</tool><tool_input>weather in SF</tool_input>\n",
    "<observation>64 degrees</observation>\n",
    "\n",
    "When you are done, respond with a final answer between <final_answer></final_answer>. For example:\n",
    "\n",
    "<final_answer>The weather in SF is 64 degrees</final_answer>\n",
    "\n",
    "Begin!\n",
    "\n",
    "Previous Conversation:\n",
    "\n",
    "\n",
    "Question: 今天广州的天气\n",
    "<tool>weather</tool><tool_input>广州</tool_input><observation>{'description': '阴', 'temperature': '21'}</observation>\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7bbe06d",
   "metadata": {},
   "source": [
    "你是一个有用的助手。 帮助用户回答任何问题。\n",
    "\n",
    "您可以使用以下工具：\n",
    "\n",
    "weather: 根据城市获取天气数据\n",
    "\n",
    "为了使用工具，您可以使用 `<tool></tool>`和 `<tool_input></tool_input>` 标签。 然后您将收到 `<observation></observation>` 形式的响应\n",
    "例如，如果您有一个名为“search”的工具，可以运行谷歌搜索，为了搜索 SF 的天气，您将响应：\n",
    "```\n",
    "<tool>search</tool><tool_input>旧金山天气</tool_input>\n",
    "<observation>64度</observation>\n",
    "```\n",
    "完成后，请在 `<final_answer></final_answer>` 之间回复最终答案。 例如：\n",
    "\n",
    "`<final_answer>`旧金山的天气温度为 64 度`</final_answer>`\n",
    "\n",
    "开始！\n",
    "\n",
    "之前的对话：\n",
    "\n",
    "\n",
    "Question: 今天广州的天气\n",
    "\n",
    "`<tool>weather</tool><tool_input>广州</tool_input><observation>{'description': '阴', 'temperature': '21'}</observation>`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c5ab86f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
